package com.hz.house.common;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.html.parser.ParserDelegator;

/**
 * 富文本解析的回调处理类
 * @author lfw
 *
 */
public class HtmlToText extends ParserCallback  {

	private List<String> sections;
	
	public static void main(String[] args) throws IOException {
		HtmlToText h =new HtmlToText();
		h.parse("1、这个是的第一条回答<p>2、这是我的第二条回答</p>");
		for(String s:h.sections)
			System.out.println(s);
		
	}
    public void handleText(char[] data, int pos) {
    	String x = new String(data);//每一个标签包裹的文本内容分段返回
    	sections.add(x);
    }
	
	public void parse(String html) throws IOException {
		sections = new ArrayList<String>();
		ParserDelegator  p =new ParserDelegator();
		Reader r = new StringReader(html);
		p.parse(r, this, true);
		r.close();
	}
	
	public List<String> getString(){
		return this.sections;
	}
}
